package com.example.moneybook.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.moneybook.dto.ResponseResult;
import com.example.moneybook.dto.StatisticsDto;
import com.example.moneybook.mapper.StatisticsMapper;
import com.example.moneybook.pojo.Bill;
import com.example.moneybook.pojo.PieData;
import com.example.moneybook.pojo.PieDetailed;
import com.example.moneybook.service.StatisticsService;
import com.example.moneybook.utils.LastDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 包
 * @Date: 2023/2/18 20:14
 * Description:
 */
@Service
public class StatisticsServiceImpl extends ServiceImpl<StatisticsMapper, Bill> implements StatisticsService {

    @Autowired
    StatisticsMapper statisticsMapper;

    /**
     * 加载统计页面明细
     * @param userId
     * @param billDate
     * @param state
     * @return
     * @throws ParseException
     */
    @Override
    public ResponseResult statisticsLoad(String userId, String billDate, int state) throws ParseException {
        StatisticsDto statisticsDto = new StatisticsDto();
        //统计当月每个类型的总收支（支出构成）
        List<PieData> pieData = statisticsMapper.pieByType(userId, billDate.substring(0,7), state);
        List<PieDetailed> pieDetailed = statisticsMapper.pieByTypeIcon(userId, billDate.substring(0,7), state);
        statisticsDto.setPieData(pieData);
        statisticsDto.setPieDetailed(pieDetailed);

        LastDateUtils lastDateUtils = new LastDateUtils();

        List dayCategories = new ArrayList();
        List dayDate = new ArrayList();
        for (int i=6; i>=0; i--){
            //根据当天日期获取前0-7天的日期
            String lastDay = lastDateUtils.getLastDay(billDate, i);
            //获取当前日期lastDay的总收支（每日对比）
            Integer money = statisticsMapper.lastDay(userId,lastDay,state);
            dayCategories.add(lastDay.substring(5,lastDay.length()));
            if (money == null){
                dayDate.add(0);
            }else {
                dayDate.add(money);
            }
        }
        statisticsDto.setDayCategories(dayCategories);
        statisticsDto.setDayData(dayDate);

        List monthCategories = new ArrayList();
        List monthDate = new ArrayList();
        for (int i=6; i>=0; i--){
            String tempMonth = billDate.substring(0,7);
            //根据当前月份获取前0-7个的月份（每月对比）
            String lastMonth = lastDateUtils.getLastMonth(tempMonth, i);
            //获取当月的总收支
            Integer money = statisticsMapper.lastMonth(userId, lastMonth, state);
            monthCategories.add(lastMonth.substring(2,7));
            if (money == null){
                monthDate.add(0);
            }else {
                monthDate.add(money);
            }
        }
        int monthMoney = (int) monthDate.get(monthDate.size()-1);
        statisticsDto.setMonthMoney(monthMoney);
        statisticsDto.setMonthCategories(monthCategories);
        statisticsDto.setMonthData(monthDate);

        List<Bill> monthDataList = new ArrayList<>();
        monthDataList = statisticsMapper.billByMonth(userId, billDate.substring(0,7), state);
        statisticsDto.setMonthDataList(monthDataList);
        return ResponseResult.okResult(statisticsDto);
    }

    /**
     * 查看某个类型的收/支明细
     * @param userId
     * @param billDate
     * @param billType
     * @param sortType
     * @param state
     * @return
     */
    @Override
    public ResponseResult billByType(String userId, String billDate, String billType, int sortType, int state) {
        List<Bill> data = new ArrayList<>();
        if (sortType == 0){
            data = billByMoney(userId, billDate, billType, state);
        }else if(sortType ==1){
            data = billByTime(userId, billDate, billType, state);
        }
        return ResponseResult.okResult(data);
    }

    /**
     * 按金额排序
     * @param userId
     * @param billDate
     * @param state
     * @return
     */
    public List<Bill> billByMoney(String userId, String billDate, String billType, int state){
        List<Bill> res = statisticsMapper.billByMoney(userId, billDate, billType, state);
        return res;
    }

    /**
     * 按时间排序
     * @param userId
     * @param billDate
     * @param state
     * @return
     */
    public List<Bill> billByTime(String userId, String billDate, String billType, int state){
        List<Bill> res = statisticsMapper.billByTime(userId, billDate, billType, state);
        return res;
    }
}
